package com.neusoft.freshmenapp.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.neusoft.freshmenapp.R;
import com.neusoft.freshmenapp.util.HttpUtil;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    //toolbar控件
    private Toolbar toolbar;
    //用户名输入框
    private EditText username;
    //密码输入框
    private EditText password;
    //登录按钮
    private Button bt_login;
    //忘记密码按钮
    private Button bt_forgetPassword;
    //注册按钮
    private Button bt_register;
    //记住密码框
    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //找到toolbar控件
        toolbar = findViewById(R.id.toolbar);
        //初始化toolbar
        setSupportActionBar(toolbar);
        //显示返回按钮
        showBackMenu();

        //找控件
        username = findViewById(R.id.et_userName);
        password = findViewById(R.id.et_password);
        bt_login = findViewById(R.id.bt_login);
        bt_forgetPassword = findViewById(R.id.bt_forgetPassword);
        bt_register = findViewById(R.id.bt_register);
        checkBox = findViewById(R.id.checkBox);

        bt_login.setOnClickListener(this);
        bt_register.setOnClickListener(this);
    }

    /**
     * 登录按钮和注册按钮点击回调
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_login:
                String url = "http://10.0.2.2:8080/freshmenapp/user/login";
              String name =  username.getText().toString();
              String pwd=  password.getText().toString();
                loginAction(url, name, pwd);
            case R.id.bt_register:
                break;


        }
    }

    /**
     * 登录请求
     * @param uri
     * @param name
     * @param pwd
     */
    private void loginAction(String uri, String name, final String pwd) {
        //通过okhttp工具类，发送登录请求

        JSONObject json = new JSONObject();
        try {
            json.put("name", name);
            json.put("password", pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        HttpUtil.request(uri, json,new Callback() {
            /**
             * 失败请求
             * @param call
             * @param e
             */
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();

            }

            /**
             * 请求成功
             * @param call
             * @param response
             * @throws IOException
             */
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //从服务器端获取到的结果集
                final String responseData = response.body().string();
                Log.d("tag", "onResponse: "+responseData);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //通过JSONArray解析返回的json数据
                            JSONObject obj=new JSONObject(responseData);
                          String result  =   obj.getString("result");
                          String role  =   obj.getString("role");

                          //当登录成功时，result的值为true
                            if (result.equals("true")){
                                username.setText("");
                                password.setText("");
                                Toast.makeText(LoginActivity.this,"登录成功,角色为"+role,Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                });
            }
        });
    }


    /**
     * 显示返回按钮
     */
    protected void showBackMenu() {
        //显示返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    /**
     * 菜单点击了回调
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            //当返回按钮点击了
            case android.R.id.home:
                //关闭当前页面
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
